package pl.register.android.widget.table;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import pl.register.android.widget.Refreshable;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-16
 */
public class StudentLessonsTable extends LinearLayout implements Refreshable {

    private StudentLessonsTableAdapter adapter;

    public StudentLessonsTable(Context context) {
        super(context, null);
        setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
    }

    public StudentLessonsTable(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setAdapter(StudentLessonsTableAdapter adapter) {
        this.adapter = adapter;
        refresh();
    }

    private void createTableContent() {
        for(int i = 0; i < adapter.getCountSection(); i++) {
            View lessonSectionView = adapter.getLessonSectionView(null, i);
            addView(lessonSectionView);
        }
    }

    @Override
    public void refresh() {
        removeAllViews();
        if (adapter != null) {
            createTableContent();
        }
    }
}